package com.example.tuter.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.tuter.R;
import com.example.tuter.activity.util.PageResultHandler;
import com.example.tuter.activity.util.PageResultHelper;
import com.example.tuter.adapter.PostAdapter;
import com.example.tuter.entity.Message;
import com.example.tuter.entity.PageResult;
import com.example.tuter.entity.Post;
import com.example.tuter.util.Constant;
import com.example.tuter.util.UpdateUIUtil;
import com.example.tuter.util.httpUtil.HttpFunction.CommunityAPI;

public class PostSearchActivity extends BaseActivity implements PageResultHandler<Post> {
    private static final String TAG = PostSearchActivity.class.getSimpleName();

    private PostAdapter searchAdapter;
    private CommunityAPI communityAPI;
    private EditText editText;
    private PageResultHelper<Post> pageHelper = new PageResultHelper<Post>();
    private ImageView imageView_back;
    private ImageView search_send;
    private RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }


    @Override
    protected void initData() {
        communityAPI = Constant.r.create(CommunityAPI.class);
    }

    @Override
    protected void bindEvent() {
        imageView_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });


        search_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                // 隐藏软键盘
                imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
                pageHelper.getFirstPageInRefreshing(PostSearchActivity.this,searchAdapter);

            }
        });


        searchAdapter.setRecyclerItemClickListener(new PostAdapter.OnRecyclerItemClickListener() {
            @Override
            public void onRecyclerItemClick(int position) {

                Intent intent = new Intent(PostSearchActivity.this, ViewPostActivity.class);

                startActivity(intent);
            }
        });
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_post_search_commulication);
        recyclerView = findViewById(R.id.search_recyclerView);
        editText = findViewById(R.id.tie_sousuo);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        searchAdapter = new PostAdapter(pageHelper.getData(), this);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(searchAdapter);
        pageHelper.addPageScroll(this, recyclerView);
        imageView_back = (ImageView) findViewById(R.id.fragment_tie_toolbar_imageView);
        //点击搜索，清空搜索结果，重新开始页面展示
        search_send = findViewById(R.id.Search_send);
    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_post_search_commulication;
    }


    @Override
    public retrofit2.Call<Message<PageResult<Post>>> getDataByPage(Integer pageNo) {
        return communityAPI.searchPost(editText.getText().toString(), Constant.PAGE_SIZE, pageNo, Constant.POST_TYPE_NONE);
    }

    @Override
    public void updateUI() {
        UpdateUIUtil.updateUIBySingleAdapter(searchAdapter);
    }


}
